package com.propertypoint.presentation.actions;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.propertypoint.presentation.model.Log;
import com.propertypoint.presentation.model.Property;
import com.propertypoint.service.business.AuditServiceImpl;
import com.propertypoint.service.business.PropertyFeedServiceImpl;
import com.propertypoint.service.general.AuditService;
import com.propertypoint.service.general.PropertyFeedService;
import com.propertypoint.util.PropertyPointConstants;

/**
 * Servlet implementation class PropertySearchServlet
 */
@WebServlet("/PropertySearchServlet")
public class PropertySearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private PropertyFeedService propertyFeedService = new PropertyFeedServiceImpl();
	private AuditService auditService=new AuditServiceImpl();
	List<Property> propertyList = new ArrayList<Property>();
    private int currentPage = 1;  
    private int itemsPerPage = 5;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PropertySearchServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	boolean isNewPropertySearch=false;
		if(request.getParameter("page") != null) {
			try {
				currentPage = Integer.parseInt(request.getParameter("page"));
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		HttpSession session = request.getSession();
		session.setAttribute("tab_status", null);
		
		if(request.getParameter("itemsPerPage") != null) {
			try {
				itemsPerPage = Integer.parseInt(request.getParameter("itemsPerPage"));
			} catch (Exception e) {
				
				e.printStackTrace();
			}
			
		}
		
		if(request.getParameter("page") == null) {
			System.out.println("new search");
			isNewPropertySearch=true;
			propertyList = propertyFeedService
				.searchProperty(request.getParameterMap());
		}
		
		
		int totalPages= propertyList.size() / itemsPerPage;
		if((propertyList.size() % itemsPerPage) > 0) {
			totalPages++;
		}
		
		List<Property> pagePropertyList =new ArrayList<Property>();
		if(propertyList.size() > 0) {
			for (int i = (currentPage-1)*itemsPerPage; i < currentPage*itemsPerPage; i++) {
				if(i <= propertyList.size()-1) {
					pagePropertyList.add(propertyList.get(i));
				}
				
			}
		}
		
		
		request.setAttribute(PropertyPointConstants.PROPERTY_LIST, pagePropertyList);
			
		request.setAttribute("totalPages", totalPages);
		request.setAttribute("itemsPerPage", itemsPerPage);
		request.setAttribute("currentPage", currentPage);
		request.setAttribute("totalRecords", propertyList.size());

		// sets the form search values to next search
		request.setAttribute(PropertyPointConstants.RENT_SALE,
				request.getParameter(PropertyPointConstants.RENT_SALE));
		request.setAttribute(PropertyPointConstants.TYPE,
				request.getParameter(PropertyPointConstants.TYPE));
		request.setAttribute(PropertyPointConstants.LOCATION,
				request.getParameter(PropertyPointConstants.LOCATION));
		request.setAttribute(PropertyPointConstants.BEDROOMS,
				request.getParameter(PropertyPointConstants.BEDROOMS));
		request.setAttribute(PropertyPointConstants.MAX_PRICE,
				request.getParameter(PropertyPointConstants.MAX_PRICE));
		if (request.getParameter(PropertyPointConstants.MIN_PRICE) != null) {
			request.setAttribute(PropertyPointConstants.MIN_PRICE,
					request.getParameter(PropertyPointConstants.MIN_PRICE));
		}

		if (isNewPropertySearch) {
			Log log = new Log();
			log.setResultCount(String.valueOf(propertyList.size()));
			log.setRentSale(request
					.getParameter(PropertyPointConstants.RENT_SALE));
			log.setBedrooms(request
					.getParameter(PropertyPointConstants.BEDROOMS));
			log.setIp(request.getRemoteAddr());
			log.setType(request.getParameter(PropertyPointConstants.TYPE));
			log.setMaxPrice(request
					.getParameter(PropertyPointConstants.MAX_PRICE));
			if (request.getParameter(PropertyPointConstants.MIN_PRICE) != null&&request.getParameter(PropertyPointConstants.MIN_PRICE).startsWith(PropertyPointConstants.ANY_PRICE)) {
				log.setMinPrice(PropertyPointConstants.ANY);
			} else {
				log.setMinPrice(request
						.getParameter(PropertyPointConstants.MIN_PRICE));
			}
			if (request.getParameter(PropertyPointConstants.LOCATION) != null) {
				log.setLocation(request
						.getParameter(PropertyPointConstants.LOCATION));
			} else {
				log.setLocation(request
						.getParameter(PropertyPointConstants.ANY));
			}
			auditService.logSearchResult(log);
		}
		RequestDispatcher rd = getServletContext().getRequestDispatcher(
				PropertyPointConstants.RESULT_PAGE);
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
	}

}
